package com.fosss.band.service.impl;

import com.fosss.band.constant.FollowType;
import com.fosss.band.entity.Band;
import com.fosss.band.entity.Follow;
import com.fosss.band.entity.dto.BandDto;
import com.fosss.band.entity.vo.AlbumVo;
import com.fosss.band.entity.vo.SongVo;
import com.fosss.band.mapper.FollowMapper;
import com.fosss.band.service.AlbumService;
import com.fosss.band.service.BandService;
import com.fosss.band.service.FollowService;
import com.fosss.band.service.SongService;
import com.fosss.band.utils.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author: fosss
 * Date: 2023/12/27
 * Time: 20:40
 * Description:
 */
@Service
public class FollowServiceImpl implements FollowService {

    @Resource
    private FollowMapper followMapper;
    @Resource
    private BandService bandService;
    @Resource
    private AlbumService albumService;
    @Resource
    private SongService songService;

    /**
     * 关注取关
     */
    @Override
    public Result followOrUnFollow(Integer userId, Integer type, Integer entityId) {
        //判断应该关注还是取关，和参加演唱会逻辑一样
        //查询有无该条记录
        Follow follow = followMapper.select(userId, type, entityId);
        if (follow == null) {
            //执行关注操作
            follow = new Follow();
            follow.setFanId(userId);
            follow.setType(type);
            follow.setEntityId(entityId);
            followMapper.insert(follow);
        } else {
            //执行取关操作
            followMapper.deleteById(follow.getId());
        }
        return Result.ok();
    }

    /**
     * 查询关注的乐队、专辑、歌曲
     */
    @Override
    public Result getFollowed(Integer userId, Integer type) {
        List<Integer> ids = followMapper.selectEntityId(userId, type);
        if (ids != null && ids.size() > 0) {
            if (type == FollowType.FOLLOW_TYPE_BAND) {
                //批量查询乐队信息
                List<BandDto> bandList = bandService.getBandDtoByIds(ids);
                return Result.ok().data("rows", bandList);
            } else if (type == FollowType.FOLLOW_TYPE_ALBUM) {
                //批量查询专辑信息
                List<AlbumVo> albumVoList = albumService.getAlbumVoByIds(ids);
                return Result.ok().data("rows", albumVoList);
            } else if (type == FollowType.FOLLOW_TYPE_SONG) {
                //批量查询歌曲信息
                List<SongVo> songVoList = songService.getSongVoByIds(ids);
                return Result.ok().data("rows", songVoList);
            }
        }
        return Result.ok();
    }
}










